/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Basketball;

import VISIE.characters.BasketballAgent;
import VISIE.characters.BasketballCharacter;
import VISIE.scenemanager.SceneCharacterManager;
import com.jme3.math.Vector3f;
import com.jme3.network.serializing.serializers.Vector3Serializer;
import java.util.ArrayList;
import VISIE.characters.Character;
import VISIE.scenemanager.Court;


/**
 *
 * @author Divesh
 */
public class DefenseDecision {
    
    BasketballAgent parentCharacter;
    private Character markedCharacter;
    
    
    public DefenseDecision(BasketballAgent ba){
        parentCharacter = ba;
    }
    
    public void makeDefenseDecision(){

      //find opponent to mark  
      int[][] pairs = this.getNearestNeighbourPairs();
      int characterToMark = -1;
      
      for(int i = 0; i < pairs.length; i++){
          if(parentCharacter.getID() == pairs[i][0]){
              characterToMark = pairs[i][1];
              break;
          }
          else if(parentCharacter.getID() == pairs[i][1]){
              characterToMark = pairs[i][0];
              break;
          }
      }
      
      //set position according to marking decision
      if(characterToMark != -1){
            Vector3f newPos = this.makeMarkingDecision(characterToMark);
            parentCharacter.planner.setTargetPosition(newPos);
            markedCharacter = SceneCharacterManager.getCharacterByID(characterToMark);
      }
      else{
          markedCharacter = null;
      }
      
      
      //sets behavior state if player is able to defend
      if(markedCharacter != null && parentCharacter.perception.isLookingAtTarget(markedCharacter.getPosition())
         && parentCharacter.planner.getTargetPosition().setY(0).distance(parentCharacter.get2DPosition()) < 3){
          parentCharacter.setBehaviorState(3);      
      }
      else{
          parentCharacter.setBehaviorState(1);
      }
      
      
      
    }
        
    private int[][] getNearestNeighbourPairs(){

      ArrayList<Character> temp = (ArrayList<Character>)SceneCharacterManager.getCharacterArray().clone();
      int[][] markingPairs = new int[temp.size()/2][2];
      ArrayList<Integer> usedIDs = new ArrayList<Integer>();
      int index = 0;
      
      for(int i = 0; i < temp.size(); i++){

            BasketballCharacter x = (BasketballCharacter)temp.get(i); //player to test
            float minDist = 1000000;
            int minID = -1;
            
            if(!usedIDs.contains(x.getID())){ //if not yet used

                for(int j = 0; j < temp.size(); j++){

                    BasketballCharacter y = (BasketballCharacter)temp.get(j);

                    if(!x.playerIsTeamMate(y) && !usedIDs.contains(y.getID())){
                        float dist = x.get2DPosition().distance(y.get2DPosition());
                        if(dist < minDist){
                            minDist = dist;
                            minID = y.getID();
                        }
                    } 
                }  

               if(minID != -1){ 
                    int[] pair = {x.getID(), minID};                     
                    markingPairs[index] = pair;
                    index++;
                    usedIDs.add(x.getID());
                    usedIDs.add(minID);
               }
            }
            
      }
      return markingPairs; 
    }
    
    private Vector3f makeMarkingDecision(int playerID){
        
        Vector3f targetPos = new Vector3f();
        Character c = SceneCharacterManager.getCharacterByID(playerID); 
        BasketballCharacter bc = (BasketballCharacter)c;
        
        
        int decision; //0 to block goal, 1 to block pass, 2 to wait at goal
        
//        if(bc.isInPossession()){
//            decision = 0;
//        }
//        else if(!bc.isInPossession() && bc.get2DPosition().distance(Court.getHoopLocation()) > 15 && parentCharacter.getTeamMates().size() == 1){
//            decision = 2;
//        }
//        else{
//            decision = 0;
//        }
        
        decision = 0;
        
        if(decision == 0){  //marking player with ball
            Vector3f toGoalVector = Court.getGoalPosition().subtract(c.getPosition()).setY(0);
            toGoalVector.normalizeLocal();
            float distFromPlayer = 5f;
            
            if(!bc.isInPossession()){
                float distToGoal = bc.get2DPosition().distance(Court.getGoalPosition().clone().setY(0));
                
                if(distToGoal > 20){
                    distFromPlayer = 15f;
                }
                else if(distToGoal < 5f){
                    distFromPlayer = 2.5f;
                }
                else{
                    distFromPlayer = 5f;
                }
            }
            
            else if(Court.getGoalPosition().clone().setY(0).distance(c.getPosition().setY(0)) < 25){
                distFromPlayer = 2.5f;
            }
            
            targetPos = c.getPosition().add(toGoalVector.mult(distFromPlayer));
        }
//        else if(decision == 2){ //marking player without ball - waits by goal
//            Vector3f toGoalVector = c.getPosition().subtract(Court.getGoalPosition()).setY(0);
//            toGoalVector.normalizeLocal();
//            float distFromGoal = 10f;
//            
//            targetPos = Court.getGoalPosition().add(toGoalVector.mult(distFromGoal));
//        
//        }
        
        return targetPos;
    
    }
    
    public Character getMarkCharacter(){
        return markedCharacter;
    }
    
}
